package com.apobates.jforum2.troll.threads.biz.impl;

import com.apobates.jforum2.troll.threads.biz.TopicConfigService;
import com.apobates.jforum2.troll.threads.biz.dao.TopicConfigDao;
import com.apobates.jforum2.troll.threads.entity.TopicConfig;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author xiaofanku
 * @since 20200513
 */
@Service
public class TopicConfigServiceImpl implements TopicConfigService{
    @Autowired
    private TopicConfigDao topicConfigDao;
    private final static Logger logger = LoggerFactory.getLogger(TopicConfigServiceImpl.class);

    //@Cacheable(value="topicCache", key="'topic_config_'+#topicId", unless="#result==null")
    @Override
    public Optional<TopicConfig> getByTopicId(long topicId) {
        return topicConfigDao.findOneByTopic(topicId);
    }
    
    @Override
    public Optional<Boolean> create(long topicId, TopicConfig config) {
        try {
            config.setTopicId(topicId);
            topicConfigDao.save(config);
            return config.getId() > 0 ? Optional.of(true) : Optional.empty();
        } catch (Exception e) {
            if(logger.isDebugEnabled()){
                logger.debug("创建话题配置文件失败", e);
            }
        }
        return Optional.empty();
    }
}